package hyce;

import HYCE.BMP;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.zip.Deflater;
import javax.imageio.ImageIO;

public class CompressionTool {

   public static byte[] compressByte (byte[]input) throws IOException{
        Deflater df = new Deflater();
        //df.setLevel(Deflater.BEST_COMPRESSION);
        df.setInput(input);

        //we write the generated byte code in this array
        ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length);
        df.finish();
        byte[] buff = new byte[1024];
        while(!df.finished())
        {
            int count = df.deflate(buff);
            baos.write(buff, 0, count);
        }
        baos.close();
        byte[] output = baos.toByteArray();

        System.out.println("Original: "+input.length);
        System.out.println("Compressed: "+output.length);
        return output;
   }

   public static byte[] imageToByteArray(BufferedImage originalImage, String imageType) throws IOException
   {
		byte[] byteArray;
		// convert BufferedImage to byte array
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ImageIO.write(originalImage, imageType, baos);
		baos.flush();
		byteArray = baos.toByteArray();
		baos.close();

		return byteArray;
   }

   public static BufferedImage byteArrayToImage(byte[] byteArray)
   {
		BufferedImage bitmap = BMP.getGrayscale((int)Math.sqrt((double)byteArray.length), byteArray);

		return bitmap;
   }

//	public static void main(String[] args) {
   public CompressionTool(){

   }
   public String compress(String imgPath, String imgType) {
        try {
                String imagePath = imgPath;
                String imageType = imgType;

                byte[] imageInByte;
                BufferedImage originalImage = ImageIO.read(new File(imagePath));

                imageInByte = imageToByteArray(originalImage, imageType);

                //*******THIS CODE MAKES THE DIFFERENCE******
                imageInByte = compressByte (imageInByte);


                // convert byte array back to BufferedImage
                //InputStream in = new ByteArrayInputStream(imageInByte);

                //BufferedImage bitmap = BMP.getGrayscale((int)Math.sqrt((double)imageInByte.length), imageInByte);
                BufferedImage bitmap = byteArrayToImage(imageInByte);


                String hycFilePath = imgPath.substring(0, imgPath.length() - 4) + "C.bmp";
                File bitmapFile = new File(hycFilePath);
//                File bitmapFile = new File("CompressedBitmap.png");
                ImageIO.write(bitmap, "png", bitmapFile);

                System.out.println("Compressed bitmap: " + bitmapFile.getAbsolutePath());

                return bitmapFile.getAbsolutePath();

        } catch (IOException e) {
                System.out.println(e.getMessage());
                return null;
        }
    }
}